//
//  LocationListViewController.m
//  collection-proto3
//
//  Created by Robert England on 10/24/10.
//  Copyright 2010 Transylvania University. All rights reserved.
//

#import "LocationListViewController.h"
//RE::24Oct2010
#import "Proto3AppController.h"
#import "LabelSettingViewController.h"
#import "ItemListViewController.h"
//::RE

@implementation LocationListViewController


#pragma mark -
#pragma mark Initialization
//RE::24Oct2010
-(id)init {
	[super initWithStyle:UITableViewStylePlain];
	
	// fetch the location list
	Proto3AppController *ac = [Proto3AppController sharedAppController];
	NSArray *list = [ac allInstancesOf:@"Location" orderedBy:@"label"];
	locationList = [list mutableCopy];
	
	[self setTitle:@"Locations"];
	
	// create the Add navigation item
	UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
																		target:self
																		  action:@selector(createNewLocation:)];
	[[self navigationItem] setRightBarButtonItem:item];
	[item release];
	
	return self;
}

// override the superclass's designated initializer
-(id)initWithStyle:(UITableViewStyle)style {
	return [self init];
}

//::RE


/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if ((self = [super initWithStyle:style])) {
    }
    return self;
}
*/

//RE::24Oct2010

#pragma mark Action methods

-(void)createNewLocation:(id)sender {
	labelSettingViewController = [[LabelSettingViewController alloc] init];
	[[self navigationController] pushViewController:labelSettingViewController animated:YES];
}

//::RE

#pragma mark -
#pragma mark View lifecycle

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
//RE::24Oct2010

-(void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	
	// am I coming back from the LabelSettingViewController?
	if (labelSettingViewController) {
		NSString *value = [labelSettingViewController value];
		
		// did the user give a value for the label?
		if ([value length] > 0) {
			Proto3AppController *ac = [Proto3AppController sharedAppController];
			NSManagedObjectContext *moc = [ac managedObjectContext];
			
			// create a new object and insert it into the managed object context
			NSManagedObject *newLoc = [NSEntityDescription insertNewObjectForEntityForName:@"Location"
																	inManagedObjectContext:moc];
			[newLoc setValue:value forKey:@"label"];
			[locationList addObject:newLoc];
			
			// resort the array
			NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"label"
															   ascending:YES];
			NSArray *sds = [NSArray arrayWithObject:sd];
			[sd release];
			[locationList sortUsingDescriptors:sds];
			
			// redisplay the table view
			[[self tableView] reloadData];
		}
		[labelSettingViewController release];
		labelSettingViewController = nil;
	}
	
	// clear the selection
	NSIndexPath *selectedPath = [[self tableView] indexPathForSelectedRow];
	if (selectedPath) {
		[[self tableView] deselectRowAtIndexPath:selectedPath animated:NO];
	}
}

//::RE
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

//RE::
/*
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return <#number of sections#>;
}
*/

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
//    return <#number of rows in section#>;
	return [locationList count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"LocationCell"; //RE:: replaced "Cell" ::RE
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    
	//RE::24Oct2010, Big Nerd p 363
	NSManagedObject *location = [locationList objectAtIndex:[indexPath row]];
	[[cell textLabel] setText:[location valueForKey:@"label"]];
	[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
	//::RE
	
    return cell;
}

//::RE

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	/*
	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
	 [self.navigationController pushViewController:detailViewController animated:YES];
	 [detailViewController release];
	 */
//RE::24Oct2010
	ItemListViewController *anotherViewController = [[ItemListViewController alloc] init];
	[anotherViewController setLocation:[locationList objectAtIndex:[indexPath row]]];
	[[self navigationController] pushViewController:anotherViewController
										   animated:YES];
	[anotherViewController release];
//::RE
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
//RE::24Oct2010
	[locationList release];
//::RE
    [super dealloc];
}


@end

